﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;
using GeometryProcessing;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace CCD
{
    public partial class TrainConsole : Form
    {

        #region GLOBAL VARIABLE

        Image<Rgb, Byte> imgAve;
        Image<Rgb, Byte> imgMin;
        Image<Rgb, Byte> imgMax;
        Image<Rgb, Byte> imgVar;

        #endregion


        string _trainPath, _modelPath;

        public TrainConsole()
        {
            InitializeComponent();
        }

        public TrainConsole(string trainPath, string modelPath)
        {

            InitializeComponent();
            _trainPath = trainPath;
            _modelPath = modelPath;

        }

        private void TrainConsole_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            string[] fns = Directory.GetFiles(tbTrainPath.Text);
            Image<Rgb, Byte> curImg = new Image<Rgb, byte>(fns[0]);
            Image<Rgb, float> sumImg = new Image<Rgb, float>(curImg.Size);
            imgAve = curImg.Clone();
            imgMin = curImg.Clone();
            imgMax = curImg.Clone();
            imgVar = curImg.Clone();

            foreach (var fn in fns)
            {
                curImg = new Image<Rgb, byte>(fn);
//                curImg = preProcess(curImg);

                #region ERROR REPORT
                if (curImg.Size != imgAve.Size)
                {
                    MessageBox.Show("图片尺寸不同！");
                    return;
                }
                #endregion

                // Generate Std Imgs
                sumImg = sumImg.Add(curImg.Convert<Rgb, float>());
                imgMin = curImg.Min(imgMin);
                imgMax = curImg.Max(imgMax);


            }

            sumImg._Mul((double)1 / fns.Length);

            imageBox1.Image = sumImg;

            sumImg.Save(Path.Combine(_modelPath, "imgAve.bmp"));
            imgMax.Save(Path.Combine(_modelPath, "imgMax.bmp"));
            imgMin.Save(Path.Combine(_modelPath, "imgMin.bmp"));
            imgVar.Save(Path.Combine(_modelPath, "imgVar.bmp"));

        }
    }
}
